package org.deva.erp.common.controller;

import java.io.Serializable;

import javax.annotation.Resource;

import org.deva.erp.base.controller.BaseBean;
import org.deva.erp.base.util.PageDateModel;
import org.deva.erp.common.entity.Role;
import org.deva.erp.common.service.RoleService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

/**
 * Project: erp<br>
 * Description: Role Backing bean.<br>
 * Copyright (c) 2012<br>
 * All Rights Reserved.<br>
 * @author <a href="mailto:gly420@163.com">GuanLuYong</a><br>
 */
@Controller @Scope("view")
public class RoleBean extends BaseBean<Role> implements Serializable {
	private static final long serialVersionUID = 1L;
	@Resource
	private RoleService roleService;
	
	public void initAddRole() {
		instance = new Role();
		instance.setStatus(1l);
	}
	
	@Override
	public void ensurePermission() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void search() {
		String prefix = "SELECT r ";
		String ttPrefix = "SELECT COUNT(r) ";
		StringBuilder suffix = new StringBuilder("FROM Role r WHERE 1=1 ");
		Object code = queryMap.get("code");
		if(code != null && !"".equals(code)) {
			suffix.append("AND r.code LIKE '" + code + "%' ");
		}
		Object name = queryMap.get("name");
		if(name != null && !"".equals(name)) {
			suffix.append("AND r.name LIKE '%" + name + "%' ");
		}
		String orderby = "ORDER BY r.code";
		lazyData = new PageDateModel<Role>(roleService, prefix, ttPrefix, suffix.toString(), orderby, null);
	}

	@Override
	public void editEntity() throws Exception {
		super.editEntity();
		roleService.update(instance);
	}

	@Override
	public void addEntity() throws Exception {
		super.addEntity();
		roleService.insert(instance);
	}

	@Override
	public void removeEntity() throws Exception {
		roleService.delete(instance);
	}
	
	// G & S
	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

}
